package com.scm.biz.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scm.biz.bean.Area;
import com.scm.biz.dto.AreaDto;
import com.scm.biz.service.AreaService;
import com.scm.core.bean.resp.RespBean;
import com.scm.core.bean.resp.RespPagination;

@Controller
@RequestMapping("/area")
public class AreaAction {
	
	@Resource
	private AreaService areaService;

	public AreaAction() {
		
	}
	
	@RequestMapping("/province")
	@ResponseBody
	public RespBean<RespPagination<AreaDto>> getProvince(){
		RespBean<RespPagination<AreaDto>> respBean = new RespBean<RespPagination<AreaDto>>();
		RespPagination<AreaDto> respPagination = new RespPagination<AreaDto>();
		List<Area> areas = areaService.getAllProvince();
		List<AreaDto> areaDtos = new ArrayList<AreaDto>();
		for(Area area: areas){
			AreaDto areaDto = new AreaDto();
			areaDto.setAreaId(area.getArea_id());
			areaDto.setAreaName(area.getArea_name());
			areaDtos.add(areaDto);
		}
		respPagination.setRows(areaDtos);
		respBean.setBody(respPagination);
		return respBean;
	}
	
	@RequestMapping("/cityOrArea")
	@ResponseBody
	public RespBean<RespPagination<AreaDto>> getArea(String parentId){
		RespBean<RespPagination<AreaDto>> respBean = new RespBean<RespPagination<AreaDto>>();
		if(parentId==null){
			return respBean;
		}
		RespPagination<AreaDto> respPagination = new RespPagination<AreaDto>();
		List<Area> areas = areaService.getAreaByParentId(Integer.parseInt(parentId));
		List<AreaDto> areaDtos = new ArrayList<AreaDto>();
		for(Area area: areas){
			AreaDto areaDto = new AreaDto();
			areaDto.setAreaId(area.getArea_id());
			areaDto.setAreaName(area.getArea_name());
			areaDtos.add(areaDto);
		}
		respPagination.setRows(areaDtos);
		respBean.setBody(respPagination);
		return respBean;
	}
	

}
